﻿<Window x:Class="Waf.DotNetPad.Presentation.Views.ShellWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ctrl="clr-namespace:Waf.DotNetPad.Presentation.Controls"
        xmlns:dd="clr-namespace:Waf.DotNetPad.Presentation.DesignData"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d" d:DataContext="{d:DesignInstance dd:SampleShellViewModel, IsDesignTimeCreatable=True}"
        Icon="{StaticResource DotNetPadIcon}" Height="500" Width="800">

    <Window.Title>
        <MultiBinding Converter="{StaticResource WindowTitleConverter}">
            <Binding Path="FileService.ActiveDocumentFile.FileName"/>
            <Binding Path="Title"/>
        </MultiBinding>
    </Window.Title>

    <Window.InputBindings>
        <KeyBinding Command="{Binding StartCommand}" Key="F5"/>
        <KeyBinding Command="{Binding FileService.OpenCommand}" Key="O" Modifiers="Control"/>
        <KeyBinding Command="{Binding FileService.CloseCommand}" Key="F4" Modifiers="Control"/>
        <KeyBinding Command="{Binding FileService.SaveCommand}" Key="S" Modifiers="Control"/>
        <KeyBinding Command="{Binding InfoCommand}" Key="F1"/>
        <KeyBinding Command="{Binding GarbageCollectorCommand}" Key="G" Modifiers="Control+Shift"/>
    </Window.InputBindings>

    <Grid x:Name="rootContainer">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition x:Name="bottomPanesRow" Height="75"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid Background="{StaticResource DarkBackground}" UseLayoutRounding="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            
            <StackPanel Grid.Column="0" Background="{StaticResource DarkBorderBrush}" Orientation="Horizontal" HorizontalAlignment="Left">
                <Button x:Name="newFileButton" Click="NewFileButtonClick" Content="New" Style="{StaticResource MenuButton}"/>
                <ctrl:Flyout x:Name="newFilePopup" PlacementTarget="{Binding ElementName=newFileButton}" Placement="Bottom"
                             Opened="NewFilePopupOpened">
                    <Border Background="{StaticResource DarkBackground}">
                        <StackPanel Background="{StaticResource DarkBorderBrush}">
                            <Button Command="{Binding FileService.NewCSharpCommand}" Click="CloseFilePopup"
                                    Content="New C# File" Width="150" Margin="0,1,0,1" Style="{StaticResource VerticalMenuButton}"/>
                            <Button Command="{Binding FileService.NewVisualBasicCommand}" Click="CloseFilePopup"
                                    Content="New VB File" Width="150" Style="{StaticResource VerticalMenuButton}"/>
                            <Button Command="{Binding FileService.NewCSharpFromClipboardCommand}" Click="CloseFilePopup"
                                    Content="New C# from Clipboard" Width="150" Style="{StaticResource VerticalMenuButton}"/>
                            <Button Command="{Binding FileService.NewVisualBasicFromClipboardCommand}" Click="CloseFilePopup" 
                                    Content="New VB from Clipboard" Width="150" Margin="0" Style="{StaticResource VerticalMenuButton}"/>
                        </StackPanel>
                    </Border>
                </ctrl:Flyout>
                <Button Command="{Binding FileService.OpenCommand}" Content="Open" Style="{StaticResource MenuButton}"/>
                <Button Command="{Binding FileService.SaveCommand}" Content="Save" Style="{StaticResource MenuButton}"/>
            </StackPanel>

            <Grid Grid.Column="1" Background="{StaticResource DarkBorderBrush}" HorizontalAlignment="Center">
                <Button Command="{Binding StopCommand}" Content="Stop" Margin="1,0" Style="{StaticResource MenuButton}"
                        Visibility="{Binding IsScriptRunning, Converter={StaticResource BoolToVisibilityConverter}}" ctrl:IconInfo.Icon="{StaticResource StopImage}"/>
                <Button Command="{Binding StartCommand}" Content="Start" Margin="1,0" Style="{StaticResource MenuButton}"
                        Visibility="{Binding IsScriptRunning, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}" ctrl:IconInfo.Icon="{StaticResource StartImage}"/>
            </Grid>

            <StackPanel Grid.Column="2" Background="{StaticResource DarkBorderBrush}" Orientation="Horizontal" HorizontalAlignment="Right">
                <Button x:Name="samplesButton" Content="Samples" Click="SamplesButtonClick" Style="{StaticResource MenuButton}" Margin="1,0"/>
                <ctrl:Flyout x:Name="samplesPopup" PlacementTarget="{Binding ElementName=samplesButton}" Placement="Bottom" HorizontalOffset="-160">
                    <Border Background="{StaticResource DarkBackground}">
                        <Grid Background="{StaticResource DarkBorderBrush}">
                            <Grid.Resources>
                                <Style TargetType="Label">
                                    <Setter Property="Foreground" Value="{StaticResource DarkForeground}"/>
                                    <Setter Property="Background" Value="{StaticResource DarkBackground}"/>
                                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                                    <Setter Property="Padding" Value="7,0"/>
                                    <Setter Property="Margin" Value="0,0,1,1"/>
                                </Style>
                            </Grid.Resources>
                            
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="160"/>
                                <ColumnDefinition Width="74"/>
                                <ColumnDefinition Width="73"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <Label Grid.Row="0" Content="Null-conditional operator" Margin="0,1,1,1"/>
                            <Button Grid.Column="1" Grid.Row="0" Command="{Binding FileService.NewCSharpCommand}" Content="C#"
                                    CommandParameter="{Binding CSharpSampleService.NullConditionalOperator}"
                                    Click="CloseSamplesPopup" Margin="0,1,1,1" Style="{StaticResource MenuButton}"/>
                            <Button Grid.Column="2" Grid.Row="0" Command="{Binding FileService.NewVisualBasicCommand}" Content="VB"
                                    CommandParameter="{Binding VisualBasicSampleService.NullConditionalOperator}"
                                    Click="CloseSamplesPopup" Margin="0,1,0,1" Style="{StaticResource MenuButton}"/>

                            <Label Grid.Row="1" Content="nameof operator"/>
                            <Button Grid.Column="1" Grid.Row="1" Command="{Binding FileService.NewCSharpCommand}" Content="C#" 
                                    CommandParameter="{Binding CSharpSampleService.NameOfOperator}"
                                    Click="CloseSamplesPopup" Margin="0,0,0,1" Style="{StaticResource MenuButton}"/>
                            <Button Grid.Column="2" Grid.Row="1" Command="{Binding FileService.NewVisualBasicCommand}" Content="VB" 
                                    CommandParameter="{Binding VisualBasicSampleService.NameOfOperator}" 
                                    Click="CloseSamplesPopup" Margin="0,0,0,1" Style="{StaticResource MenuButton}"/>

                            <Label Grid.Row="2" Content="Auto-property initializers"/>
                            <Button Grid.Column="1" Grid.Row="2" Command="{Binding FileService.NewCSharpCommand}" Content="C#" 
                                    CommandParameter="{Binding CSharpSampleService.AutoPropertyInitializers}"
                                    Click="CloseSamplesPopup" Margin="0,0,0,1" Style="{StaticResource MenuButton}"/>
                            <Button Grid.Column="2" Grid.Row="2" Command="{Binding FileService.NewVisualBasicCommand}" Content="VB" 
                                    CommandParameter="{Binding VisualBasicSampleService.AutoPropertyInitializers}" 
                                    Click="CloseSamplesPopup" Margin="0,0,0,1" Style="{StaticResource MenuButton}"/>

                            <Label Grid.Row="3" Content="String interpolation" Margin="0,0,1,0"/>
                            <Button Grid.Column="1" Grid.Row="3" Command="{Binding FileService.NewCSharpCommand}" Content="C#" 
                                    CommandParameter="{Binding CSharpSampleService.StringInterpolation}"
                                    Click="CloseSamplesPopup" Margin="0,0,1,0" Style="{StaticResource MenuButton}"/>
                            <Button Grid.Column="2" Grid.Row="3" Command="{Binding FileService.NewVisualBasicCommand}" Content="VB" 
                                    CommandParameter="{Binding VisualBasicSampleService.StringInterpolation}" 
                                    Click="CloseSamplesPopup" Margin="0" Style="{StaticResource MenuButton}"/>
                        </Grid>
                    </Border>
                </ctrl:Flyout>
                
                <Button Command="{Binding InfoCommand}" Content="Info" Style="{StaticResource MenuButton}"/>
                
                <Button x:Name="moreButton" Content=". . ." Click="MoreButtonClick" Style="{StaticResource MenuButton}" Margin="0"/>
                <ctrl:Flyout x:Name="morePopup" PlacementTarget="{Binding ElementName=moreButton}" Placement="Bottom" HorizontalOffset="-77">
                    <Border Background="{StaticResource DarkBackground}">
                        <StackPanel Background="{StaticResource DarkBorderBrush}">
                            <Button Command="{Binding FileService.SaveAsCommand}" Content="Save As" 
                                    Width="150" Margin="0,1,0,1" Style="{StaticResource VerticalMenuButton}"/>
                            <Button Command="{Binding FileService.CloseAllCommand}" Content="Close All Documents" 
                                    Click="CloseMorePopup" Width="150" Margin="0" Style="{StaticResource VerticalMenuButton}"/>
                        </StackPanel>
                    </Border>
                </ctrl:Flyout>
            </StackPanel>
        </Grid>

        <TabControl Grid.Row="1" ItemsSource="{Binding DocumentDataModels}" SelectedItem="{Binding ActiveDocumentDataModel}" Padding="0" BorderThickness="0,1,0,0">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock VerticalAlignment="Center" Margin="5,0" ToolTip="{Binding DocumentFile.FileName}">
                                        <TextBlock.Text>
                                            <MultiBinding Converter="{StaticResource TabFileNameConverter}">
                                                <Binding Path="DocumentFile.FileName"/>
                                                <Binding Path="DocumentFile.Modified"/>
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>
                                    <Button Command="{Binding DataContext.FileService.CloseCommand, ElementName=rootContainer}"
                                        Visibility="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}, Converter={StaticResource BoolToVisibilityConverter}}"
                                        Width="16" Height="16" Margin="0,1,0,0" Background="Transparent" BorderBrush="Transparent">
                                        <Path Data="M 0,0 L 7,7 M 7,0 L 0,7" StrokeThickness="2" Stroke="Gray"/>
                                    </Button>
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ContentControl Focusable="False">
                                    <ContentControl.Style>
                                        <Style TargetType="ContentControl">
                                            <Style.Triggers>
                                                <Trigger Property="IsVisible" Value="True">
                                                    <Setter Property="Content" Value="{Binding LazyCodeEditorView.Value}"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </ContentControl.Style>
                                </ContentControl>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.ItemContainerStyle>
        </TabControl>

        <GridSplitter Grid.Row="2" Background="{StaticResource DarkBackground}" Height="6" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>

        <ContentControl Grid.Row="3" Content="{Binding OutputView}" Visibility="{Binding IsOutputViewVisible, Converter={StaticResource BoolToVisibilityConverter}}" 
                        Focusable="False"/>

        <ContentControl Grid.Row="3" Content="{Binding ErrorListView}" Visibility="{Binding IsErrorListViewVisible, Converter={StaticResource BoolToVisibilityConverter}}"
                        Focusable="False"/>

        <Border Grid.Row="4" TextElement.Foreground="{StaticResource DarkForeground}" Background="{StaticResource DarkBackground}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="65"/>
                    <ColumnDefinition Width="10"/>
                    <ColumnDefinition Width="65"/>
                    <ColumnDefinition Width="10"/>
                </Grid.ColumnDefinitions>

                <StackPanel DockPanel.Dock="Left" Background="{StaticResource DarkBorderBrush}" Orientation="Horizontal" HorizontalAlignment="Left">
                    <ToggleButton Content="Error List" IsChecked="{Binding IsErrorListViewVisible}" Height="25" Style="{StaticResource MenuButton}"/>
                    <ToggleButton Content="Output" IsChecked="{Binding IsOutputViewVisible}" Height="25" Style="{StaticResource MenuButton}"/>
                </StackPanel>
                <TextBlock Text="{Binding StatusText}" Grid.Column="2" Margin="3" VerticalAlignment="Center"/>
                <TextBlock Text="{Binding ShellService.Line, StringFormat=Ln {0}}" Grid.Column="4" Margin="3" VerticalAlignment="Center"/>
                <TextBlock Text="{Binding ShellService.Column, StringFormat=Co {0}}" Grid.Column="6" Margin="3" VerticalAlignment="Center"/>
            </Grid>
        </Border>
    </Grid>
</Window>
